﻿<%@ WebHandler Language="C#" Class="RbSpecialItemPlanCountDashBoard" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ReportBusiness;
using System.Net;

/*
 * Description: 热表厂专项计划统计看板
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2025.2.26
 */

public class RbSpecialItemPlanCountDashBoard : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    TreeObject t = new TreeObject();
    List<TreeObject> treeNodes = new List<TreeObject>();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//柱状图
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "getDetails":
                        r = getDetails(jObject);//查看明细
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportData":
                        getExportExcel(jObject);//完成率--导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getDetailSql(JObject jObject)
    {
        string sql = @"
SELECT t.itemresource,t.itemcontent,t.itemfinishprogress,t.needfinishdate,t.finishdate,t.responseteam,
        CASE WHEN t.finishdate IS NULL THEN '未完成'
            WHEN t.finishdate IS NOT NULL AND TRUNC(t.finishdate) <= TRUNC(t.needfinishdate) THEN '按时完成'
            ELSE '超期完成' 
        END status,
        CASE WHEN t.finishdate IS NULL AND TRUNC(SYSDATE) > TRUNC(t.needfinishdate) THEN TRUNC(SYSDATE) - TRUNC(t.needfinishdate)
            WHEN t.finishdate IS NOT NULL AND TRUNC(t.finishdate) > TRUNC(t.needfinishdate) THEN TRUNC(finishdate) - TRUNC(t.needfinishdate)
            ELSE 0
        END delayday
FROM rbspeciallyitemplan t WHERE t.responseteam IS NOT NULL";
        //项目来源
        if (jObject["itemresource"] != null && jObject["itemresource"].ToString() != "")
        {
            sql += string.Format(" AND t.itemresource = '{0}'", jObject["itemresource"].ToString());
        }
        //计划完成时间
        if (jObject["needfinishdate"] != null && jObject["needfinishdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["needfinishdate"].ToString());
            sql += string.Format(" AND TRUNC(t.needfinishdate) >= to_date('{0}','yyyy/mm/dd') AND TRUNC(t.needfinishdate) <= to_date('{1}','yyyy/mm/dd')",
                                 arr[0].ToString(), arr[1].ToString());
        }

        //项目来源
        if (jObject["name"] != null && jObject["name"].ToString() != "")
        {
            sql += string.Format(" AND t.responseteam = '{0}'", jObject["name"].ToString());
        }

        if (jObject["seriesName"] != null)
        {
            string seriesName = jObject["seriesName"].ToString();
            if (seriesName == "完成率")
            {
                sql += " AND t.finishdate IS NOT NULL";
            }
            else if (seriesName == "按时完成率")
            {
                sql += " AND t.finishdate IS NOT NULL AND TRUNC(t.finishdate) <= TRUNC(t.needfinishdate)";
            }
            else if (seriesName == "未完成率")
            {
                sql += " AND t.finishdate IS NULL";
            }
            else if (seriesName == "超期天数")
            {
                sql += @" AND ((t.finishdate IS NULL AND TRUNC(SYSDATE) > TRUNC(t.needfinishdate)) 
OR (t.finishdate IS NOT NULL AND TRUNC(t.finishdate) > TRUNC(t.needfinishdate)))";
            }
        }
        return sql;
    }

    Result getData(JObject jObject)
    {
        string sql = @"
SELECT m.responseteam,COUNT(1) total,
       SUM(m.delayday) delaydays,
       SUM(DECODE(m.status,'未完成',0,1)) finishqty,
       SUM(DECODE(m.status,'未完成',1,0)) notfinishqty,
       SUM(DECODE(m.status,'按时完成',1,0)) intimefinishqty,
       SUM(DECODE(m.status,'超期完成',1,0)) delayfinishqty,
       ROUND(SUM(DECODE(m.status,'未完成',0,1)) / COUNT(1),4) * 100 finishrate,
       ROUND(SUM(DECODE(m.status,'未完成',1,0)) / COUNT(1),4) * 100 notfinishrate,
       ROUND(SUM(DECODE(m.status,'按时完成',1,0)) / COUNT(1),4) * 100 intimefinishrate,
       ROUND(SUM(DECODE(m.status,'超期完成',1,0)) / COUNT(1),4) * 100 delayfinishrate
FROM (
    {0}
) m
GROUP BY m.responseteam";
        sql = string.Format(sql, getDetailSql(jObject));
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dt.Rows.Count);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    Result getDetails(JObject jObject)
    {
        string sql = getDetailSql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    void getExportExcel(JObject jObject)
    {
        string sql = getDetailSql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dt.Columns["itemresource"].Caption = "项目来源";
        dt.Columns["itemcontent"].Caption = "项目内容";
        dt.Columns["itemfinishprogress"].Caption = "措施";
        dt.Columns["needfinishdate"].Caption = "要求完成时间";
        dt.Columns["finishdate"].Caption = "完成时间";
        dt.Columns["responseteam"].Caption = "责任单位";
        dt.Columns["status"].Caption = "状态";
        dt.Columns["delayday"].Caption = "超期天数";
        ExportHelper.ExportExcel(dt, jObject["title"].ToString());
    }
}